package com.sx.pipeline.ui.supply.frag

import android.os.Bundle
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.FragReleaseSupplyBinding
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.ui.supply.ReleaseSupplyVM


/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class ReleaseSupplyFragment :
    CYDataBindingBaseFragment<FragReleaseSupplyBinding, ReleaseSupplyVM>(R.layout.frag_release_supply),View.OnClickListener {
    private lateinit var mAdapter: ReleaseSupplyListAdapter
    private var status = 0 //任务状态

    companion object {
        fun newInstance(position: Int) =
            ReleaseSupplyFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        status = arguments?.getInt(IntentKey.ID)!!
        mViewModel.pushSupplyPage(status)
    }


    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = ReleaseSupplyListAdapter()
            mBinding.recyclerView.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as ReleaseSupplyVo
            RouteManager.Mine.intentToSupplyDetail(info.id)
        }
        mBinding.recyclerView.adapter = mAdapter

        //取消发布
        mAdapter.cancelReleaseCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_cancel_post_supply_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                       mViewModel.cancelSupplyOrder(it)
                    }, null, false
                ).show()
        }
        //取消供应
        mAdapter.cancelSupplyCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_cancel_post_supply_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                        mViewModel.cancelSupplyOrder(it)
                    }, null, false
                ).show()
        }
        //删除任务
        mAdapter.deleteTaskCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_delete_supply_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                        mViewModel.deleteSupplyOrder(it)
                    }, null, false
                ).show()
        }
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.releaseSupplyListEvent.observe(this) {
            initRv()

            val taskList = it as List<ReleaseSupplyVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<ReleaseSupplyVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

        observe(mViewModel.successSupplyEvent) {
            if (it == true) {
                mViewModel.page = 1
                mViewModel.pushSupplyPage(status)
            }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.pushSupplyPage(status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.pushSupplyPage(status)
            }
        })

    }


    override fun onClick(v: View?) {
        when (v?.id) {
            //跳转发布供应
            R.id.tv_add -> {
                RouteManager.Mine.intentToReleaseSupplyAdd()
            }
        }
    }


}